# coding=utf-8
from src.db.dbhelper import df_to_model_list
from src.mapper.usermapper import get_user, del_user_by_id_mapper, add_user_mapper
from src.response.LayR import LayR


def get_user_list(*args, **kwargs):
    page = kwargs.get("page")
    limit = kwargs.get("limit")
    username = kwargs.get("username")
    layR = LayR()
    df, total = get_user(page=page, limit=limit, username=username)
    if df.empty:
        return layR.ok(data=[])
    items = df_to_model_list(df)
    return layR.ok(data=items, count=total)


def del_user_by_id(*args, **kwargs):
    u_id = kwargs.get("id")
    layR = LayR()
    print(id)
    res = del_user_by_id_mapper(id=u_id)
    if res:
        return layR.ok(code=200, msg="删除成功")
    return layR.error(msg="删除失败")


def add_user_service(*args, **kwargs):
    uname = kwargs.get("uname")
    email = kwargs.get("email")
    content = kwargs.get("content")
    password = kwargs.get("password")
    layR = LayR()
    res = add_user_mapper(uname=uname, email=email, content=content, password=password)
    if res:
        return layR.ok(msg="新增成功", code=200)
    return layR.ok(msg="新增失败", code=500)


def update_user_service(*args, **kwargs):
    uname = kwargs.get("uname")
    email = kwargs.get("email")
    content = kwargs.get("content")
    password = kwargs.get("password")
    uid = kwargs.get("uid")
    layR = LayR()
    res = update_user_mapper(uname=uname, email=email, content=content, password=password, uid=uid)
    if res:
        return layR.ok(msg="修改成功", code=200)
    return layR.ok(msg="修改失败", code=500)